﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xaml;
using System.Windows.Markup;
using System.ComponentModel;

namespace CollectionViewSourceSpikes.Controls {
    public class TypeExtension : IMarkupExtension<Type> {

        public string Target { get; set; }

        public Type ProvideValue(IServiceProvider serviceProvider) {
            if (string.IsNullOrWhiteSpace(this.Target)) {
                return null;
            }
            Type result = Type.GetType(this.Target) as Type;
            if (result != null) {
                return result;
            }
            IXamlTypeResolver resolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
            try {
                return resolver.Resolve(this.Target);
            } catch (NotSupportedException) {
            }
            
            return null;
        }
    }
}
